<?php
/**
 * COD Payment Module
 *
 * @package paymentMethod
 * @copyright Copyright 2003-2010 Zen Cart Development Team
 * @copyright Portions Copyright 2003 osCommerce
 * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
 * @version $Id: cod.php 15420 2010-02-04 21:27:05Z drbyte $
 */
    
  class hipay  extends base{
    var $code, $title, $description, $enabled, $userAccountId, $signKey, $categoryId, $siteId, $return_url, $cancel_url;

// class constructor
    function hipay() {
      global $order;  
      
      $this->code = 'hipay';
      $this->title = MODULE_PAYMENT_HIPAY_TEXT_TITLE;
      $this->description = MODULE_PAYMENT_HIPAY_TEXT_DESCRIPTION;      
      $this->enabled = ((MODULE_PAYMENT_HIPAY_STATUS == 'True') ? true : false);
      
      $this->form_action_url 	= "https://test-payment.hipay.com/index/form/";
      $this->return_url = "http://www.zc.com/index.php?main_page=checkout_success";//local
      //$this->return_url = "https://www.suplementos-online.com/serverdev/index.php?main_page=checkout_success";//serverdev
      //$this->return_url = "https://www.suplementos-online.com/index.php?main_page=checkout_success";//live
      
      $this->return_url = "http://www.zc.com/index.php?main_page=checkout_process";//local
      //$this->return_url = "https://www.suplementos-online.com/serverdev/index.php?main_page=checkout_process";;//serverdev
      //$this->return_url = "https://www.suplementos-online.com/index.php?main_page=checkout_process";//live
      
      $this->userAccountId = "104134";//account test
      
      $this->signKey = "bc2453f863b245ba1c4458ba4eb25b3d";//local
      //$this->signKey = "18fb9bcbe4366670f976de8bac9c7543";//serverdev
      $this->categoryId = "643";//local
      //$this->categoryId = "665";//serverdev
      $this->siteId = "5141";//local
      //$this->siteId = "5140";//serverdev
    }

// class methods
    function update_status() {
      global $order, $db;
      if ( ($this->enabled == true) && ((int)MODULE_PAYMENT_HIPAY_ZONE > 0) ) {
        $check_flag = false;
        $check = $db->Execute("select zone_id from " . TABLE_ZONES_TO_GEO_ZONES . " where geo_zone_id = 6 and zone_country_id = '" . $order->delivery['country']['id'] . "' order by zone_id");
        while (!$check->EOF) {
          if ($check->fields['zone_id'] < 1) {
            $check_flag = true;
            break;
          } elseif ($check->fields['zone_id'] == $order->delivery['zone_id']) {
            $check_flag = true;
            break;
          }
          $check->MoveNext();
        }

        if ($check_flag == false) {            
          $this->enabled = false;//comment for testing
        }
      }

// disable the module if the order only contains virtual products
      if ($this->enabled == true) {
        if ($order->content_type != 'physical') {            
          $this->enabled = false;//comment for testing
        }
      }
    }

    function javascript_validation() {
      return false;
    }

    function selection() {
        global $order;        
        //if($order->delivery['country']['id'] === 30)
        {
            return array('id' => $this->code,
                        'module' => $this->title);
        }
    }

    function pre_confirmation_check() {
      return false;
    }

    function confirmation() {
      return false;
    }

    function process_button() {
        global $order;        
        
        $xml = "<?xml version='1.0' encoding='utf-8' ?> 
                <order> 
                    <userAccountId>" . $this->userAccountId . "</userAccountId> 
                    <currency>EUR</currency> 
                    <label>Order In Suplementos-online.com</label> 
                    <ageGroup>ALL</ageGroup> 
                    <categoryId>".$this->categoryId."</categoryId> 
                    <urlAcquital><![CDATA[http://www.exemple.com/acquittement.php]]></urlAcquital> 
                    <urlOk>". $this->return_url ."</urlOk> 
                    <urlKo><![CDATA[http://www.exemple.com/ko.php]]></urlKo> 
                    <urlCancel>" . $this->cancel_url . "</urlCancel> 
                    <urlInstall><![CDATA[http://www.exemple.com/install.php]]></urlInstall> 
                    <urlLogo><![CDATA[http://www.exemple.com/logo.gif]]></urlLogo> 
                    <locale>pt_PT</locale> 
                    <issuerAccountLogin>" . $order->customer['email_address'] . "</issuerAccountLogin>  
                    <items>";
        $hasShipping = false;
        
        for($i = 0; $i < sizeof($order->products); $i++)
        {
            $id = $i + 1;
            
            $xml .=     "<item id='" . (string)$id . "'> 
                            <name>" . $order->products[$i]['name'] . "</name> 
                            <infos>" . $order->products[$i]['model'] . "</infos> 
                            <amount>" . $order->products[$i]['final_price'] . "</amount> 
                            <categoryId>" . $this->categoryId . "</categoryId> 
                            <quantity>" . $order->products[$i]['qty'] . "</quantity> 
                            <reference>REF" . (string)$id . "</reference> 
                            <taxes>";
            
            if(!$hasShipping)
            {
                $xml .=        "<tax id='1'> 
                                    <label>shipping</label> 
                                    <amount>" . ($order->info['shipping_cost'] + ($order->info['shipping_cost'] * $order->info['shipping_tax']/100)) . "</amount> 
                                </tax>";
                $hasShipping = true;
            }
            $xml .=            "<tax id='2'> 
                                    <label>VAT</label> 
                                    <amount>" . $order->products[$i]['tax'] . "%</amount> 
                                </tax> 
                            </taxes> 
                        </item>";
        }
        $xml .=     "</items>
                </order>";
        //echo($xml);
        $data = trim($xml); 
 
        // your website Hipay key         
        $encodedData = base64_encode($data); 
        $md5Sign = md5($encodedData.$this->signKey); 
        
        $process_button_string =    zen_draw_hidden_field('mode', 'MODE_B') . "\n" .
                                    zen_draw_hidden_field('website_id', $this->siteId) . "\n" .
                                    zen_draw_hidden_field('sign', $md5Sign) . "\n" .
                                    zen_draw_hidden_field('data', $encodedData);
      return $process_button_string;
    }

    function before_process() {
      return false;
    }

    function after_process() {
      return false;
    }

    function get_error() {
      return false;
    }

    function check() {
      global $db;
      if (!isset($this->_check)) {
        $check_query = $db->Execute("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_PAYMENT_HIPAY_STATUS'");
        $this->_check = $check_query->RecordCount();
      }
      return $this->_check;
    }

    function install() {
      global $db;
      
      $db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Enable Hipay Module', 'MODULE_PAYMENT_HIPAY_STATUS', 'True', 'Do you want to accept Hipay payments?', '6', '1', 'zen_cfg_select_option(array(\'True\', \'False\'), ', now())");
      $db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, use_function, set_function, date_added) values ('Payment Zone', 'MODULE_PAYMENT_HIPAY_ZONE', '0', 'If a zone is selected, only enable this payment method for that zone.', '6', '2', 'zen_get_zone_class_title', 'zen_cfg_pull_down_zone_classes(', now())");      
   }

    function remove() {
      global $db;
      $db->Execute("delete from " . TABLE_CONFIGURATION . " where configuration_key in ('" . implode("', '", $this->keys()) . "')");
    }

    function keys() {
      return array('MODULE_PAYMENT_HIPAY_STATUS','MODULE_PAYMENT_HIPAY_ZONE');
    }
  }
